package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TeRecordService;
import com.iknight.theory.model.TPRecordModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/record")
@Api(value = "电量数据记录控制器")
public class RecordController implements BaseController<TPRecordModel> {

    private final static Logger logger = LogManager.getLogger(RecordController.class);

    @Autowired
    private TeRecordService<TPRecordModel, String> teRecordService;

    /**
     * 格式化数据
     *
     * @param params
     * @return
     */
    @Override
    public TPRecordModel format(Map<String, Object> params) {
        TPRecordModel entity = new TPRecordModel();
        entity.setObj_id(IkgStringUtils.ObjectToString(params.get("obj_id")));

        if (IkgStringUtils.isNotNull(params.get("equip_id"))) {
            String equip_id = IkgStringUtils.ObjectToString(params.get("equip_id"));
            entity.setEquip_id(equip_id);
        }
        if (IkgStringUtils.isNotNull(params.get("equip_type"))) {
            String equip_type = IkgStringUtils.ObjectToString(params.get("equip_type"));
            entity.setEquip_type(equip_type);
        }
        if (IkgStringUtils.isNotNull(params.get("data_date"))) {
            String data_date = IkgStringUtils.ObjectToString(params.get("data_date"));
            entity.setData_date(data_date);
        }
        if (IkgStringUtils.isNotNull(params.get(""))) {
            String data_type = IkgStringUtils.ObjectToString(params.get("data_type"));
            entity.setData_type(data_type);
        }
        if (IkgStringUtils.isNotNull(params.get(""))) {
            String data_value = IkgStringUtils.ObjectToString(params.get("data_value"));
            entity.setData_value(Double.valueOf(data_value.equals("") ? "0" : data_value));
        }

        return entity;
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teRecordService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation("删除方法")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("obj_id"));
            teRecordService.delete(pKey);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }

        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation("单条记录查询")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("obj_id"));
            TPRecordModel entity = teRecordService.selectOne(pKey);
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation("列表分页查询")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        PageInfo<TPRecordModel> pageInfo = teRecordService.selectPage(params, pageIndex, pageSize);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("/getConsPowerByParamInPage")
    @ApiOperation("根据查询参数分页查询用户电量")
    public Object getConsPowerByParamInPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));

        Map param = new HashMap();
        param.put("pagestart", pageIndex);
        param.put("pagesize", pageSize);
        if (null != params.get("tg_id")) {
            param.put("tg_id", params.get("tg_id"));
        }
        if (null != params.get("tran_id")) {
            param.put("tran_id", params.get("tran_id"));
        }
        PageInfo<Map> pageInfo = teRecordService.getConsPowerByParam(param);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("/getTranPowerByPage")
    @ApiOperation("分页查询配变电量")
    public Object getTranPowerByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        Map param = new HashMap();
        param.put("pagestart", pageIndex);
        param.put("pagesize", pageSize);
        if (null != params.get("tg_id")) {
            param.put("tg_id", params.get("tg_id"));
        }
        PageInfo<Map> pageInfo = teRecordService.getTranPowerData(param);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("/removePowerDataByRecordId")
    @ApiOperation("根据记录ID删除记录数据")
    public Object removePowerDataByRecordId(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        String recordId = IkgStringUtils.ObjectToString(params.get("record_id"));
        teRecordService.removePowerDataByRecord(recordId);
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @PostMapping("/getSegPowerInPage")
    @ApiOperation("分页获取分段电量数据")
    public Object getSegPowerInPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        String recordId = IkgStringUtils.ObjectToString(params.get("record_id"));
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        Map param = new HashMap();
        param.put("pagestart", pageIndex);
        param.put("pagesize", pageSize);
        param.put("record_id", recordId);

        PageInfo<Map> pageInfo = teRecordService.getSegPowerByRecord(param);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("/getCalLogByRecordId")
    @ApiOperation("根据记录ID获取计算日志")
    public Object getCalLogByRecordId(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        String recordId = IkgStringUtils.ObjectToString(params.get("record_id"));
        return IkgWebUtils.postIkgResponse(true, "查询成功!", teRecordService.getCalLogByRecordId(recordId));
    }

    @PostMapping("/getTranPowerCurveByParam")
    @ApiOperation(value = "根据查询参数获取电量数据曲线", httpMethod = "POST")
    public Object getTranPowerCurveByParam(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        List<Map> resultLs = teRecordService.getTranPowerByParam(params);
        Map result = new HashMap();
        result.put("count",resultLs.size());
        result.put("result",resultLs);
        return IkgWebUtils.postIkgResponse(true, "查询成功", result);
    }

    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && IkgStringUtils.isNotNull(params.get("obj_id"));
    }
}
